package org.skydiveapphd;

import org.skydiveapphd.ui.FragmentHelper;
import org.skydiveapplibrary.R;
import org.skydiveapplibrary.repository.LocationRepository;
import org.skydiveapplibrary.ui.LocationListAdapter;

import android.os.Bundle;
import android.view.View;
import android.widget.ListView;

public class LocationListActivity extends BaseListFragment
{
    @Override 
    public void onCreate(Bundle savedInstanceState)
    { 
    	super.onCreate(savedInstanceState);
    }
    
    @Override
    public void onResume()
    {
    	super.onResume();
    	
    	getListView().setChoiceMode(ListView.CHOICE_MODE_NONE);
    	setHeaderText(R.string.locations_label);
    	setBottomButtonImage(R.drawable.ic_menu_revert);
    	
    	loadData();
    }
    
    @Override
    public void onListItemClick(ListView lv, View view, int position, long id)
    {
    	// Create new fragment and transaction
    	Bundle args = new Bundle();
    	args.putInt(Constants.EXTRA_LOCATION_ID, (int)id);
		
    	FragmentHelper.getInstance().openFragment(LocationEditActivity.newInstance(args), android.R.id.tabhost, 
				Constants.TAG_LOCATION_EDIT, getFragmentManager());
    }
    
    private void loadData()
    {
    	// populate
		LocationRepository repository = new LocationRepository(getActivity());
		LocationListAdapter adapter = new LocationListAdapter(
				getActivity(),
				repository.getLocations());
		setListAdapter(adapter);
    }
    
    @Override
    public void onTopButtonClick()
    {	
    	FragmentHelper.getInstance().openFragment(LocationEditActivity.newInstance(new Bundle()), android.R.id.tabhost, 
				Constants.TAG_LOCATION_EDIT, getFragmentManager());
    }
    
    @Override
    protected void onBottomButtonClick()
    {
    	// finish
		getFragmentManager().popBackStack();
    }
} 